/*------------------------------

	Space Is The Place v 0.1
		by Sestze
		
	cl_menu.lua

------------------------------*/

function SITPHelp()
	local szx = 600
	local szy = 400
	local px = 10
	local origin = {}
		origin.x = (ScrW() - szx) / 2
		origin.y = (ScrH() - szy) / 2

	local Menu = vgui.Create("DFrame")
		Menu:SetPos(origin.x, origin.y)
		Menu:SetSize(szx, szy)
		Menu:SetTitle("Space Is The Place! - Help")
		Menu:SetDraggable( false )
		Menu:ShowCloseButton( true )
		Menu:MakePopup()
	
	local title = 
		"Welcome to the Space Is The Place Gamemode!\n" ..
		"---------------\n"
	
	local living = 
		"Living in space is incredibly easy. The SITP Core Tool creates cores that, when\n" ..
		"constrained to props, provide life support to pods and a walkable environment to \n" ..
		"props in space, as long as the cumulative weight of those props is less than the cumulative \n" ..
		"tonnage grade of the constrained cores. That means, if your contraption weighs 199,000 \n" ..
		"and has a 200 ton core aboard, you can survive in space within that spaceship. However, \n" ..
		"a 20 ton core can't promise the same.\n"
		
	local surviving = 
		"Surviving in space is a different matter. Cores are incredibly volatile, and shots from ACF \n" ..
		"weaponry will cause your core to go critical, leading to the core detonating. If a core detonates, \n" ..
		"it performs a constraint check for other cores, and if the mass of the entire contraption exceeds \n" ..
		"the ton grade of the remaining cores, each core will, in turn, stackpole, causing the entire contraption \n" ..
		"to explode in a glorious fashion. Coloring your core makes the core have that explosion color.\n"
	
	local asteroids = 
		"Of course, being able to shoot people in the face requires credits, a new addition to SITP. \n" ..
		"To get credits, you mine asteroids or debris from destroyed contraptions using the SITP Mining.\n" ..
		"laser. Credits are persistent, and shared across both GGG SITP Servers. It's impossible to be locked\n" ..
		"on a planet by virtue of being broke, as you will be restored to a minimum of 10,000 credits when you respawn.\n" ..
		"\n" ..
		"Lastly, teams now have a purpose. The FSA gets a discount on weapon and ammunition purchases, \n" ..
		"the GMC gets a discount on core and mining tool purchases, and Mercs keep everything they mine. \n" ..
		"The factions take 70% of the asteroid's worth and distribute 10% to teammates, providing an \n" ..
		"incentive to guard them from other factions or mercenaries.\n" ..
		"\n" ..
		"This is still a very early build of Space Is The Place, and we're constantly making updates to it. \n" ..
		"You can leave a suggestion or bug report using !note, donate with !donate and !adonate, and find your\n" ..
		"contraption's cost with !cost\n"
		
	local Title = vgui.Create("DLabel", Menu)
		Title:SetText ( title )
		Title:SizeToContents()
		Title:SetPos( 15, 24 )
	
	local SectionA = vgui.Create("DLabel", Menu)
		SectionA:SetText ( living )
		SectionA:SizeToContents()
		SectionA:SetPos( 15, 24 + 3 * px )
		
	local SectionB = vgui.Create("DLabel", Menu)
		SectionB:SetText ( surviving )
		SectionB:SizeToContents()
		SectionB:SetPos( 15, 24 + 3 * px + 9 * px )
	
	local SectionC = vgui.Create("DLabel", Menu)
		SectionC:SetText ( asteroids )
		SectionC:SizeToContents()
		SectionC:SetPos( 15, 24 + 3 * px + 9 * px + 8 * px )
end

usermessage.Hook( "SITPHelp", SITPHelp )


function GetTeamCounts()
	local Teams = {}
	local allplys = player.GetAll()
	
	Teams.FSA = 0
	Teams.GMC = 0
	Teams.Merc = 0
	
	for k, v in pairs(allplys) do
		v.sitp_team = v:GetNetworkedString("sitp_team")
		if(v.sitp_team == "merc") then
			Teams.Merc = Teams.Merc + 1
		elseif(v.sitp_team == "fsa") then
			Teams.FSA = Teams.FSA + 1
		elseif(v.sitp_team == "gmc") then
			Teams.GMC = Teams.GMC + 1
		end
	end
	
	return Teams
end

function SITPTeam()
	local szx = 600
	local szy = 400
	local px = 20 + 4
	local origin = {}
		origin.x = (ScrW() - szx) / 2
		origin.y = (ScrH() - szy) / 2
		
	local Teams = GetTeamCounts()

	local Menu = vgui.Create("DFrame")
		Menu:SetPos(origin.x, origin.y)
		Menu:SetSize(szx, szy)
		Menu:SetTitle("Space Is The Place! - Teams")
		Menu:SetDraggable( false )
		Menu:ShowCloseButton( true )
		Menu:MakePopup()
	
	--600 from left
	-- 50, 150, 25, 150, 25, 150, 50
	local FSA = vgui.Create("Button", Menu)
		FSA:SetSize( 150, 60 )
		FSA:SetText( "Join FSA!\n\nPlayers: " .. tostring(Teams.FSA) )
		FSA:SetVisible( true )
		FSA:SetPos(125 - 75, szy / 2 - 30)
		function FSA:OnMousePressed()
			if(LocalPlayer():GetNetworkedString("sitp_team") != "fsa") then
				LocalPlayer():ChatPrint("You Have Joined the FSA!")
				RunConsoleCommand( "SITP_FSA" )
				Menu:Close()
			end
		end
	
	local Merc = vgui.Create("Button", Menu)
		Merc:SetSize( 150, 60 )
		Merc:SetText( "Join Mercs!\n\nPlayers: " .. tostring(Teams.Merc) )
		Merc:SetVisible( true )
		Merc:SetPos(300 - 75, szy / 2 - 30)
		function Merc:OnMousePressed()
			if(LocalPlayer():GetNetworkedString("sitp_team") != "merc") then
				LocalPlayer():ChatPrint("You Have Joined the Mercs!")
				RunConsoleCommand( "SITP_Merc" )
				Menu:Close()
			end
		end
	
	local GMC = vgui.Create("Button", Menu)
		GMC:SetSize( 150, 60 )
		GMC:SetText( "Join GMC!\n\nPlayers: " .. tostring(Teams.GMC) )
		GMC:SetVisible( true )
		GMC:SetPos(475 - 75, szy / 2 - 30)
		function GMC:OnMousePressed()
			if(LocalPlayer():GetNetworkedString("sitp_team") != "gmc") then
				LocalPlayer():ChatPrint("You Have Joined the GMC!")
				RunConsoleCommand( "SITP_GMC" )
				Menu:Close()
			end
		end
	
end

usermessage.Hook( "SITPTeam", SITPTeam )